﻿using System.Linq;
using System.Web.Mvc;
using IterationLife.ContentResources.Queries;
using IterationLife.Extensions.ControllerBase;
using IterationLife.PlatformBackEnd.AppServices.Contracts.ContentResources;
using IterationLife.PlatformBackEnd.AppServices.Dtos.ContentResources;
using Util.Ui.Trees;

namespace IterationLife.PlatformBackEnd.WebSite.Areas.ContentResources.Controllers
{
    /// <summary>
    /// 内容资源分类控制器
    /// </summary>
    public class CategoryController : TreeGridControllerBase<ContentResourceCategoryDto, ContentResourceCategoryQuery>
    {
        /// <summary>
        /// 初始化内容资源分类控制器
        /// </summary>
        /// <param name="service">内容资源分类服务</param>
        public CategoryController(IContentResourceCategoryService service)
            : base(service)
        {
            NewsInformationCategoryService = service;
        }

        /// <summary>
        /// 内容资源分类服务
        /// </summary>
        protected IContentResourceCategoryService NewsInformationCategoryService { get; private set; }

        /// <summary>
        /// 获取新实体
        /// </summary>
        /// <param name="parentId">父Id</param>
        public override ActionResult New(string parentId)
        {
            var dto = NewsInformationCategoryService.Create(parentId);
            dto.Enabled = true;
            return ToJsonResult(dto);
        }

        /// <summary>
        /// 树形列表
        /// </summary>
        public ActionResult ComboTree()
        {
            var dtos = NewsInformationCategoryService.GetAll();
            var nodes = dtos.Select(t => new TreeNode { Id = t.Id, Text = t.Name, ParentId = t.ParentId, Level = t.Level, State = t.State });
            return ToTreeResult(nodes);
        }

    }
}